//
//  HZPeiziWindow.m
//  Stock_Stategy
//
//  Created by MayerF on 2017/11/23.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZPeiziWindow.h"

@interface HZPeiziWindow()
@property (nonatomic, strong)UIImageView *pImageView;
@property (nonatomic, assign)BOOL isMoved;

@property (nonatomic, assign)CGPoint begainPoint;
@end

@implementation HZPeiziWindow

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
//        self.backgroundColor = [UIColor redColor];
        [self initCommon];
        [self initConstraints];
    }
    return self;
}
- (void)initCommon {
    UIViewController *vc = [UIViewController new];
    self.rootViewController = vc;
    self.hidden = NO;
    
    _pImageView = [UIImageView new];
    _pImageView.userInteractionEnabled = YES;
    _pImageView.image = [UIImage imageNamed:@"vip"];
    _pImageView.contentMode = UIViewContentModeScaleAspectFit;
}
- (void)initConstraints
{
    [self addSubview:_pImageView];
    
    [_pImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    [super touchesBegan:touches withEvent:event];
    UIWindow *keyWin = [UIApplication sharedApplication].keyWindow;
    UITouch *touch = [touches anyObject];
    _begainPoint = [touch locationInView:keyWin];
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    [super touchesMoved:touches withEvent:event];
    UIWindow *keyWin = [UIApplication sharedApplication].keyWindow;
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:keyWin];
    if (fabs(point.x - _begainPoint.x) > 3 || fabs(point.y - _begainPoint.y) > 3) {
        _isMoved = YES;
    }
    
    self.center = point;
    self.bounds = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    [super touchesEnded:touches withEvent:event];
    DLog(@"333");
    CGFloat pointY = self.center.y;
    if (pointY < self.frame.size.height/2) {
        pointY = self.frame.size.height/2;
    }else if (pointY > kScreenH - self.frame.size.height/2) {
        pointY = kScreenH - self.frame.size.height/2;
    }
    self.center = CGPointMake(kScreenW - self.frame.size.width/2, pointY);
    self.bounds = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    
    if (!_isMoved) {
        self.hidden = YES;
        UIViewController *nextVC = [[MYRouter shared] matchController:kRouterPeiziHome];
        [_baseVC.navigationController pushViewController:nextVC animated:YES];
    }
    _isMoved = NO;
}

@end
